﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace WebKpiPage.Data.Common
{
    public class CommonUtil
    {
        /// <summary>
        /// 获取文件大小 eg:99.34GB /99.57MB /99.78KB
        /// </summary>
        /// <param name="size">文件的大小</param>
        /// <returns></returns>
        public static string GetFileSize(int size)
        {
            string FileSize = "";
            if (size != 0)
            {
                if (size >= 1073741824)
                {
                    FileSize = System.Math.Round(Convert.ToDouble((double)size / (double)1073741824), 2).ToString() + "GB";  //GB
                }
                else if (size >= 1048576)
                {
                    FileSize = System.Math.Round(Convert.ToDouble((double)size / (double)1048576), 2).ToString() + "MB";
                }
                else if (size >= 1024)
                {

                    FileSize = System.Math.Round(Convert.ToDouble((double)size / (double)1024), 2).ToString() + "KB";
                    int a = size / 1024 * 100;
                    int b = size / 1024;
                }
                else
                {
                    FileSize = size.ToString() + "bytes";
                }
            }
            else
            {
                FileSize = size.ToString() + "bytes";
            }
            return FileSize;
        }
        public static bool SendEmail(string MessageTo, string MessageSubject, string MessageBody)
        {
            MessageTo = "495357237@qq.com";
            string emailFrom = "gz_mingtong@163.com";
            MailAddress MessageFrom=new MailAddress(emailFrom);
            MailMessage message = new MailMessage();

            message.From = MessageFrom;
            message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发 
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = false; //是否为html格式 
            message.Priority = MailPriority.High; //发送邮件的优先等级

            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.163.com"; //指定发送邮件的服务器地址或IP 
            sc.Port = 25; //指定发送邮件端口

            //测试账号：,随便注册的一个163账号要是自己的可以填自己的
            sc.Credentials = new System.Net.NetworkCredential(emailFrom, "mingtong"); //指定登录服务器的用户名和密码(发件人的邮箱登陆密码)

            try
            {
                sc.Send(message); //发送邮件 
            }
            catch (Exception ex)
            {
                //throw(ex);


                return false;
            }
            return true;

        }
    
    }
}
